package jmu.cultural_tourism.service.impl;

import jmu.cultural_tourism.entity.SysUserinfo;
import jmu.cultural_tourism.dao.SysUserinfoDao;
import jmu.cultural_tourism.service.SysUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;


/*用户表(SysUserinfo)表服务实现类*/
@Service("sysUserinfoService")
public class SysUserinfoServiceImpl implements SysUserinfoService {
    @Autowired
    private SysUserinfoDao sysUserinfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserinfo queryById(Integer userId) {
        return this.sysUserinfoDao.queryById(userId);
    }

    @Override
    public SysUserinfo queryByAccount(String account) {
        return this.sysUserinfoDao.queryByAccount(account);
    }

    /**
     * 分页查询
     *
     * @param sysUserinfo 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserinfo> queryByPage(SysUserinfo sysUserinfo, PageRequest pageRequest) {
        long total = this.sysUserinfoDao.count(sysUserinfo);
        return new PageImpl<>(this.sysUserinfoDao.queryAllByLimit(sysUserinfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserinfo 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserinfo insert(SysUserinfo sysUserinfo) {
        this.sysUserinfoDao.insert(sysUserinfo);
        return sysUserinfo;
    }

    /**
     * 修改数据
     *
     * @param sysUserinfo 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserinfo update(SysUserinfo sysUserinfo) {
        this.sysUserinfoDao.update(sysUserinfo);
        return this.queryById(sysUserinfo.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.sysUserinfoDao.deleteById(userId) > 0;
    }
}
